const { ListNode } = require('../../utils/list.js')

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function (head) {
  if (!head) return head
  var copy = new ListNode() // 创建一条头指针为空的链
  copy.next = head

  var cur = copy.next
  var prev = copy
  var flag = false

  while (cur.next) {
    if (cur.val == cur.next.val) {
      flag = true
    } else {
      if (flag) {
        prev.next = cur.next
        flag = false
      }
      else {
        prev = cur
      }
    }
    cur = cur.next
  }

  if (flag) prev.next = cur.next // 有可能一直遍历到末尾而未触发删除
  return copy.next
};
